<?php
	
    class SignUpEngine
    {
		private $template;
		private $dbProvider;
        
        public function __construct()
        {
            $this->template = new TemplateEngine();
			$this->dbProvider = new UserDataProvider();
        }
		
		public function checkSet()
        {
            $isSet = ( ( isset( $_POST['firstName'] ) ) && ( isset( $_POST['lastName'] ) ) && ( isset( $_POST['email'] ) )
					&& ( isset( $_POST['reEmail'] ) ) && ( isset( $_POST['password'] ) ) && ( isset( $_POST['sex'] ) )
					&& ( isset( $_POST['month'] ) ) && ( isset( $_POST['day'] ) ) && ( isset( $_POST['year'] ) ) );
					
			if ( $isSet )
			{
				return true;
			}
			else
			{
				throw new Exception( $this->template->convertToHtml( "Parametres from form not passed!" ) );
				return false;
			}			
        }
		
		private function checkEmpty()
		{
			$isValid = ( ( !empty( $_POST['firstName'] ) ) && ( !empty( $_POST['lastName'] ) ) && ( !empty( $_POST['email'] ) )
					  && ( !empty( $_POST['reEmail'] ) ) && ( !empty( $_POST['password'] ) ) && ( $_POST['sex'] != "Select Sex:" )
					  && ( $_POST['month'] != "Month:" ) && ( $_POST['day'] != "Day:" ) && ( $_POST['year'] != "Year:" ) );
			return $isValid;
		}

        public function checkInput()
        {
			if ( $this->checkEmpty() )
			{
				$isSimilar = ( strtolower( $_POST['email'] ) == strtolower( $_POST['reEmail'] ) );
				if ( $isSimilar ) 
				{
					return true;
				}
				else
				{
					throw new Exception( $this->template->convertToHtml( "Re-entering of email was incorrect!" ) );
					return false;
				}
			}
			else
			{
				throw new Exception( $this->template->convertToHtml( "Not all data is entered!" ) );
				return false;
			}
        }

        private function printToFile( $filename, $firstName, $lastName, $email, $password, $sex, $month, $day, $year )
        {
            $file = fopen( "uploaded/".$filename, "w" );

            fwrite( $file, $firstName."\n" ); 
            fwrite( $file, $lastName."\n" ); 
            fwrite( $file, $email."\n" ); 
            fwrite( $file, $password."\n" ); 
            fwrite( $file, $sex."\n" ); 
            fwrite( $file, $month."\n" ); 
            fwrite( $file, $day."\n" ); 
            fwrite( $file, $year."\n" ); 

            fclose( $file );
        }
		
		public function addToBase( $firstName, $lastName, $email, $password, $sex, $month, $day, $year )
        {
            $email = strtolower( $email );
			$filename = $email.'.txt';
			
            if ( $this->dbProvider->checkRepeatEmailInDb( $email ) )
            {
                $this->dbProvider->saveUserDataInDb( $firstName, $lastName, $email, $password, $sex, $month, $day, $year );
				$this->dbProvider->saveFilenameInDb( $filename, $firstName, $lastName, $email );
				$this->printToFile( $filename, $firstName, $lastName, $email, $password, $sex, $month, $day, $year );
				return true;
            }
            else
            {
                throw new Exception( $this->template->convertToHtml( "This email is already exist in database!" ) );
				return false;
            }
        }
	}
?>